package org.jeecg.modules.demo.shenhe.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.shenhe.entity.HywyReportProcessApproveRecord;
import org.jeecg.modules.demo.shenhe.service.IHywyReportProcessApproveRecordService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.summary.entity.MonthlySummary;
import org.jeecg.modules.demo.summary.service.IMonthlySummaryService;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: hywy_report_process_approve_record
 * @Author: jeecg-boot
 * @Date:   2024-04-08
 * @Version: V1.0
 */
@Api(tags="hywy_report_process_approve_record")
@RestController
@RequestMapping("/shenhe/hywyReportProcessApproveRecord")
@Slf4j
public class HywyReportProcessApproveRecordController extends JeecgController<HywyReportProcessApproveRecord, IHywyReportProcessApproveRecordService> {
	@Autowired
	private IHywyReportProcessApproveRecordService hywyReportProcessApproveRecordService;
	 @Autowired
	 private ISysUserService sysUserService;
	 @Autowired
	 private IMonthlySummaryService monthlySummaryService;
	 @Autowired
	 private ISysDictItemService sysDictItemService;

	/**
	 * 分页列表查询
	 *
	 * @param hywyReportProcessApproveRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "hywy_report_process_approve_record-分页列表查询")
	@ApiOperation(value="hywy_report_process_approve_record-分页列表查询", notes="hywy_report_process_approve_record-分页列表查询")
	@GetMapping(value = "/list")
	public Result<List<HywyReportProcessApproveRecord>> queryPageList(HywyReportProcessApproveRecord hywyReportProcessApproveRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HywyReportProcessApproveRecord> queryWrapper = QueryGenerator.initQueryWrapper(hywyReportProcessApproveRecord, req.getParameterMap());
		Page<HywyReportProcessApproveRecord> page = new Page<HywyReportProcessApproveRecord>(pageNo, pageSize);
		QueryWrapper<HywyReportProcessApproveRecord> monthlySummaryQueryWrapper = new QueryWrapper<>();
		monthlySummaryQueryWrapper
				.eq("business_key",hywyReportProcessApproveRecord.getBusinessKey())
				.orderByDesc("create_time");
		List<HywyReportProcessApproveRecord> list = hywyReportProcessApproveRecordService.list(monthlySummaryQueryWrapper);
		IPage<HywyReportProcessApproveRecord> pageList = hywyReportProcessApproveRecordService.page(page, queryWrapper);
		for (HywyReportProcessApproveRecord reportProcessApproveRecord : list) {
			//设置报表名称
			String businessKey = reportProcessApproveRecord.getBusinessKey();
			QueryWrapper<MonthlySummary> monthlySummaryQueryWrapper1 = new QueryWrapper<>();
			monthlySummaryQueryWrapper1.eq("id",businessKey);
			List<MonthlySummary> list2 = monthlySummaryService.list(monthlySummaryQueryWrapper1);
			reportProcessApproveRecord.setBusinessKey(list2.get(0).getReportName());
			//设置审核结果
			String approveResult = reportProcessApproveRecord.getApproveResult();
			QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
			sysDictItemQueryWrapper.eq("dict_id","1777277840205713410").eq("item_value",approveResult);
			List<SysDictItem> list1 = sysDictItemService.list(sysDictItemQueryWrapper);
			reportProcessApproveRecord.setApproveResult(list1.get(0).getItemText());
			//设置审批人
			String approver = reportProcessApproveRecord.getApprover();
			QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
			sysUserQueryWrapper.eq("id",approver);
			List<SysUser> sysUserList = sysUserService.list(sysUserQueryWrapper);
			reportProcessApproveRecord.setApprover(sysUserList.get(0).getRealname());
		}
//		return Result.OK(pageList);
		return Result.OK(list);
	}

	/**
	 *   添加
	 *
	 * @param hywyReportProcessApproveRecord
	 * @return
	 */
	@AutoLog(value = "hywy_report_process_approve_record-添加")
	@ApiOperation(value="hywy_report_process_approve_record-添加", notes="hywy_report_process_approve_record-添加")
	@RequiresPermissions("shenhe:hywy_report_process_approve_record:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody HywyReportProcessApproveRecord hywyReportProcessApproveRecord) {
		//获取角色设置任务名称
		SysRole roleOfUser = monthlySummaryService.getRoleOfUser(hywyReportProcessApproveRecord.getApprover());
		String roleName = roleOfUser.getRoleName();
		if (StrUtil.isEmpty(hywyReportProcessApproveRecord.getTaskName())){
			hywyReportProcessApproveRecord.setTaskName(roleName+"发起审核");
		}
		hywyReportProcessApproveRecordService.save(hywyReportProcessApproveRecord);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hywyReportProcessApproveRecord
	 * @return
	 */
	@AutoLog(value = "hywy_report_process_approve_record-编辑")
	@ApiOperation(value="hywy_report_process_approve_record-编辑", notes="hywy_report_process_approve_record-编辑")
	@RequiresPermissions("shenhe:hywy_report_process_approve_record:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody HywyReportProcessApproveRecord hywyReportProcessApproveRecord) {
		//添加审核逻辑 只能审核自己的
		hywyReportProcessApproveRecordService.updateById(hywyReportProcessApproveRecord);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "hywy_report_process_approve_record-通过id删除")
	@ApiOperation(value="hywy_report_process_approve_record-通过id删除", notes="hywy_report_process_approve_record-通过id删除")
	@RequiresPermissions("shenhe:hywy_report_process_approve_record:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		hywyReportProcessApproveRecordService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "hywy_report_process_approve_record-批量删除")
	@ApiOperation(value="hywy_report_process_approve_record-批量删除", notes="hywy_report_process_approve_record-批量删除")
	@RequiresPermissions("shenhe:hywy_report_process_approve_record:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hywyReportProcessApproveRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "hywy_report_process_approve_record-通过id查询")
	@ApiOperation(value="hywy_report_process_approve_record-通过id查询", notes="hywy_report_process_approve_record-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<HywyReportProcessApproveRecord> queryById(@RequestParam(name="id",required=true) String id) {
		HywyReportProcessApproveRecord hywyReportProcessApproveRecord = hywyReportProcessApproveRecordService.getById(id);
		if(hywyReportProcessApproveRecord==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hywyReportProcessApproveRecord);
	}

	 @ApiOperation(value="hywy_report_process_approve_record-通过id查询", notes="hywy_report_process_approve_record-通过id查询")
	 @GetMapping(value = "/queryById2")
	 public Result<List<HywyReportProcessApproveRecord>> queryById2(@RequestParam(name="id",required=true) String id) {
		 QueryWrapper<HywyReportProcessApproveRecord> queryWrapper = new QueryWrapper<>();
		 // 设置查询条件，例如业务键为某个值
		 queryWrapper.eq("business_key", id);

		 // 执行查询并返回结果
		 List<HywyReportProcessApproveRecord> list = hywyReportProcessApproveRecordService.list(queryWrapper);
		 // 返回结果
		 Result result = new Result();
		 result.setSuccess(true);
		 result.setResult(list);
		 return result;
//		 return (Result<List<HywyReportProcessApproveRecord>>) list;

	 }

    /**
    * 导出excel
    *
    * @param request
    * @param hywyReportProcessApproveRecord
    */
    @RequiresPermissions("shenhe:hywy_report_process_approve_record:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HywyReportProcessApproveRecord hywyReportProcessApproveRecord) {
        return super.exportXls(request, hywyReportProcessApproveRecord, HywyReportProcessApproveRecord.class, "hywy_report_process_approve_record");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("shenhe:hywy_report_process_approve_record:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HywyReportProcessApproveRecord.class);
    }

}
